package com.dick.base.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dick.base.aop.OperateLog;
import com.dick.base.dto.OrderDetail;
import com.dick.base.model.BusinessOrder;
import com.dick.base.model.BusinessOrderInfo;
import com.dick.base.model.BusinessProduceItem;
import com.dick.base.parameter.ExportOrderParameter;
import com.dick.base.parameter.ItemModifyParameter;
import com.dick.base.parameter.ItemRemoveParameter;
import com.dick.base.parameter.OrderParameter;
import com.dick.base.service.OrderService;
import com.dick.base.session.util.SessionUtil;
import com.dick.base.util.BaseResult;
import com.dick.base.util.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 订单接口
 */
@RestController
@RequestMapping("api/orders")
public class OrdersApi {

    private static final String TAG = "订单";

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "新增", tags = TAG)
    @PostMapping()
    public BaseResult<Void> saveOrder(@RequestBody @Valid OrderParameter parameter) {
        orderService.saveOrder(parameter);
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "分页查询", tags = TAG)
    @GetMapping
    public BaseResult<Page<BusinessOrderInfo>> orderPage(BusinessOrderInfo order, PageInfo pageInfo) {
        return BaseResult.of(orderService.orderPage(order, pageInfo));
    }

    @ApiOperation(value = "详情", tags = TAG)
    @GetMapping("{id}")
    public BaseResult<OrderDetail> orderDetail(@PathVariable Long id) {
        return BaseResult.of(orderService.orderDetail(id));
    }

    @ApiOperation(value = "订单项追踪", tags = TAG)
    @GetMapping("items")
    public BaseResult<Page<BusinessProduceItem>> itemPage(BusinessProduceItem produceItem, PageInfo pageInfo) {
        return BaseResult.of(orderService.itemPage(produceItem, pageInfo));
    }

    @ApiOperation(value = "发布", tags = TAG)
    @PostMapping("publish")
    public BaseResult<Void> publishOrder(@RequestParam Long id) {
        orderService.publishOrder(id);
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "作废", tags = TAG)
    @PostMapping("remove")
    public BaseResult<Void> removeOrder(@RequestParam Long id) {
        orderService.removeOrder(id);
        return BaseResult.voidResult();
    }
    @ApiOperation(value = "出库", tags = TAG)
    @PostMapping("transfer")
    public BaseResult<Void> transferOrder(@RequestParam Long id) {
        orderService.transferOrder(id);
        return BaseResult.voidResult();
    }
    @ApiOperation(value = "导出excel", tags = TAG)
    @PostMapping("export-excel")
    public void export(@RequestBody @Valid ExportOrderParameter parameter) {
        orderService.exportOrder(parameter.getOrderIdList());
    }
    @ApiOperation(value = "删除订单项", tags = TAG)
    @PostMapping("items-remove")
    public BaseResult<Void> removeItem(@RequestBody @Valid ItemRemoveParameter parameter) {
        orderService.removeItem(parameter);
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "修改订单项", tags = TAG)
    @OperateLog(operate = "修改订单项",
            detail = "T(com.dick.base.util.JsonUtil).serialize(#parameter)")
    @PostMapping("items-modify")
    public BaseResult<Void> itemModify(@RequestBody @Valid ItemModifyParameter parameter) {
        orderService.itemModify(parameter);
        return BaseResult.voidResult();
    }
}
